package me.vudu.Conflict.Runnables;

import java.util.ArrayList;
import java.util.Random;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.getspout.spoutapi.SpoutManager;

import me.vudu.Conflict.ConflictPlugin;
import me.vudu.Conflict.util.BlockLocation;

public class VirusRunnable implements Runnable{

	private ConflictPlugin plugin;
	Random generator;
	//boolean blocked;
	
	public VirusRunnable(ConflictPlugin plugin){
		this.plugin = plugin;
		generator = new Random();
		
	
	}
	
	@Override
	public void run() {
		
		if(ConflictPlugin.blocked){
			return;
		}
		
		if(null== ConflictPlugin.blueBase){
			return;
		}
		if(!ConflictPlugin.virusPlaced){
			return;
		}
		
		ConflictPlugin.blocked=true;
		
		
		ArrayList<BlockLocation> temp = new ArrayList<BlockLocation>();
		
		int numContam = ConflictPlugin.contaminated.size();
		
			
				BlockLocation blo = null;
				Block b = null;
				int r=0;
				
				for(int i=0; i<Math.min(250, numContam); i++){
					r = generator.nextInt(10);
					if(r<=4){
						blo = ConflictPlugin.contaminated.get(i);
						b = plugin.getServer().getWorlds().get(0).getBlockAt(blo.getX(), blo.getY(), blo.getZ());
						SpoutManager.getMaterialManager().overrideBlock(b, ConflictPlugin.virusBlock);
						temp.add(blo);	
					}
				    
				}
			
				 
				
			
		
		
		
//		for(BlockLocation bl: ConflictPlugin.contaminated){
//			Block b = plugin.getServer().getWorlds().get(0).getBlockAt(bl.getX(), bl.getY(), bl.getZ());
//			int r = generator.nextInt(9);
//			if(r<=4){
//				 SpoutManager.getMaterialManager().overrideBlock(b, ConflictPlugin.virusBlock);
//				 temp.add(bl);
//			}
//		}
		if(null!= ConflictPlugin.blueBase){
			
		
		for(BlockLocation bl: temp){
			
			Location l2 = new Location(plugin.getServer().getWorlds().get(0), ConflictPlugin.blueBase.getX(), ConflictPlugin.blueBase.getY(), ConflictPlugin.blueBase.getZ());
			
				 if(plugin.getServer().getWorlds().get(0).getHighestBlockAt(bl.getX()+1, bl.getZ()).getTypeId()!=ConflictPlugin.VIRUSBLOCKID){
					 //BlockLocation bl2 = new BlockLocation(plugin.getServer().getWorlds().get(0).getHighestBlockAt(bl.getX()+1, bl.getZ()).getLocation());
					 BlockLocation bl2 = new BlockLocation(plugin.getServer().getWorlds().get(0).getName(),  bl.getX()+1, plugin.getServer().getWorlds().get(0).getHighestBlockYAt(bl.getX()+1, bl.getZ())-1, bl.getZ());
					 Location l = new Location(plugin.getServer().getWorlds().get(0), bl.getX(), bl.getY(), bl.getZ());
					 if(!ConflictPlugin.contaminated.contains(bl2) && l.distance(l2)<1.5*ConflictPlugin.virusDistance)
					 ConflictPlugin.contaminated.add(bl2);	 
				 }
				 
				 if(plugin.getServer().getWorlds().get(0).getHighestBlockAt(bl.getX()-1, bl.getZ()).getTypeId()!=ConflictPlugin.VIRUSBLOCKID){
					 //BlockLocation bl2 = new BlockLocation(plugin.getServer().getWorlds().get(0).getHighestBlockAt(bl.getX()-1, bl.getZ()).getLocation());
					 BlockLocation bl2 = new BlockLocation(plugin.getServer().getWorlds().get(0).getName(),  bl.getX()-1, plugin.getServer().getWorlds().get(0).getHighestBlockYAt(bl.getX()-1, bl.getZ())-1, bl.getZ());
					 Location l = new Location(plugin.getServer().getWorlds().get(0), bl.getX(), bl.getY(), bl.getZ());
					 if(!ConflictPlugin.contaminated.contains(bl2) && l.distance(l2)<1.5* ConflictPlugin.virusDistance)
					 ConflictPlugin.contaminated.add(bl2);	 
				 }
				 
				 if(plugin.getServer().getWorlds().get(0).getHighestBlockAt(bl.getX(), bl.getZ()+1).getTypeId()!=ConflictPlugin.VIRUSBLOCKID){
					 //BlockLocation bl2 = new BlockLocation(plugin.getServer().getWorlds().get(0).getHighestBlockAt(bl.getX(), bl.getZ()+1).getLocation());
					 BlockLocation bl2 = new BlockLocation(plugin.getServer().getWorlds().get(0).getName(),  bl.getX(), plugin.getServer().getWorlds().get(0).getHighestBlockYAt(bl.getX(), bl.getZ()+1)-1, bl.getZ()+1);
					 Location l = new Location(plugin.getServer().getWorlds().get(0), bl.getX(), bl.getY(), bl.getZ());					 
					 if(!ConflictPlugin.contaminated.contains(bl2)  && l.distance(l2)< 1.5*ConflictPlugin.virusDistance)
					 ConflictPlugin.contaminated.add(bl2);	 
				 }
				 
				 if(plugin.getServer().getWorlds().get(0).getHighestBlockAt(bl.getX(), bl.getZ()-1).getTypeId()!=ConflictPlugin.VIRUSBLOCKID){
					 //BlockLocation bl2 = new BlockLocation(plugin.getServer().getWorlds().get(0).getHighestBlockAt(bl.getX(), bl.getZ()-1).getLocation());
					 BlockLocation bl2 = new BlockLocation(plugin.getServer().getWorlds().get(0).getName(),  bl.getX(), plugin.getServer().getWorlds().get(0).getHighestBlockYAt(bl.getX(), bl.getZ()-1)-1, bl.getZ()-1);
					 Location l = new Location(plugin.getServer().getWorlds().get(0), bl.getX(), bl.getY(), bl.getZ());
					 if(!ConflictPlugin.contaminated.contains(bl2)  && l.distance(l2)< 1.5*ConflictPlugin.virusDistance)
					 ConflictPlugin.contaminated.add(bl2);	 
				 }
				 
				 //Block darunter
				 if(bl.getY()>1){
					 BlockLocation bl2 = new BlockLocation(bl.getWorld(), bl.getX(), bl.getY()-1, bl.getZ());
					
					 if(!ConflictPlugin.contaminated.contains(bl2))
					 ConflictPlugin.contaminated.add(bl2);	
				 }
				  
				
				 
				 
				 ConflictPlugin.contaminated.remove(bl);
			
		}
		}
		ConflictPlugin.blocked = false;
		
	}
}
